package com.ruoyi.zhgd.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.zhgd.domain.ZjSysProjectNumber;
import com.ruoyi.zhgd.mapper.ZjOuterMapper;
import com.ruoyi.zhgd.mapper.ZjSysProjectNumberMapper;
import com.ruoyi.zhgd.service.IZjOuterService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页Service业务层处理
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjOuterServiceImpl implements IZjOuterService {

    private final ZjOuterMapper baseMapper;

    private final ZjSysProjectNumberMapper zjSysProjectNumberMapper;

    /**
     * 设备数量统计
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, List<Map<String, Object>>> deviceCount(Map<String, Object> params) {
        String projectNumber = params.get("projectNumber").toString();
        String sectionNumber = null;
        if (ObjectUtils.isNotEmpty(params.get("sectionNumber"))) {
            sectionNumber = params.get("sectionNumber").toString();
        }
        // 根据项目编号和标段编号查询一致状态
        QueryWrapper<ZjSysProjectNumber> wrapper = new QueryWrapper<>();
        wrapper.eq("project_number", projectNumber);
        wrapper.eq("section_number", sectionNumber);
        wrapper.eq("del_flag", "0");
        ZjSysProjectNumber entity = zjSysProjectNumberMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(entity) && entity.getStatus() == 1) {
            // 1表示标段编号与系统实际组织机构不对应
            params.replace("sectionNumber", null);
        }
        List<Map<String, Object>> labDeviceCount = baseMapper.labDeviceCount(params); // 试验室设备
        List<Map<String, Object>> mixDeviceCount = baseMapper.mixDeviceCount(params); // 拌合站设备
        // @TODO 待办 张拉压浆设备
        List<Map<String, Object>> tensionDeviceCount = baseMapper.tensionDeviceCount(params);// 张拉设备
        List<Map<String, Object>> groutingDeviceCount = baseMapper.groutingDeviceCount(params); // 压浆设备

        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        map.put("lab", labDeviceCount);
        map.put("mix", mixDeviceCount);
        map.put("tension", tensionDeviceCount);
        map.put("grouting", groutingDeviceCount);

        return map;
    }
}
